﻿package core.ui.dialogs
{
    import core.enum.*;
    import core.event.*;
    import core.image.*;
    import core.ui.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;

    public class NewImageDialog extends Dialog
    {
        var preset:Dropdown;
        var tbn:TextInput;
        var tbw:Dropslider;
        var tbh:Dropslider;
        var transparent:Checkbox;
        var clipboard:Checkbox;

        public function NewImageDialog(param1:Tuyitu)
        {
            super(param1, "rm_new_image", 240, 260, true);
            var _loc_2:* = new Literal("rm_name");
            _loc_2.x = 18;
            _loc_2.y = 30;
            addChild(_loc_2);
            this.tbn = new TextInput(200, 100, U.RM("rm_untitled"));
            this.tbn.x = 20;
            this.tbn.y = 47;
            addChild(this.tbn);
            var _loc_3:* = new Literal("rm_presets");
            _loc_3.x = 18;
            _loc_3.y = 75;
            addChild(_loc_3);
            this.preset = new Dropdown(200, 200);
            this.preset.addEventListener(SelectEvent.COMMAND, this.PresetChange);
            this.preset.AddItem("240 x 320", "240|320");
            this.preset.AddItem("640 x 480", "640|480");
            this.preset.AddItem("800 x 600", "800|600", true);
            this.preset.AddItem("1024 x 768", "1024|768");
            this.preset.AddItem("横幅  468 x 60", "468|60");
            this.preset.AddItem("排行榜  728 x 90", "728|90");
            this.preset.AddItem("摩天大楼 120 x 600", "120|600");
            this.preset.AddItem("宽幅摩天大楼 160 x 600", "160|600");
            this.preset.AddItem("矩形 180 x 150", "180|150");
            this.preset.AddItem("中等矩形 300 x 250", "300|250");
            this.preset.AddItem("大矩形 336 x 280", "336|280");
            this.preset.AddItem("电影画布小  480p", "720|480");
            this.preset.AddItem("电影画布中  720p", "1280|720");
            this.preset.AddItem("电影画布大  1080p", "1920|1080");
            this.preset.x = 20;
            this.preset.y = 92;
            this.transparent = new Checkbox("rm_transparent");
            this.transparent.x = 20;
            this.transparent.y = 170;
            addChild(this.transparent);
            if (param1.clipboard.bitmap != null)
            {
                this.preset.AddItem(U.RM("rm_clipboard") + " " + param1.clipboard.bitmap.width + " x " + param1.clipboard.bitmap.height, param1.clipboard.bitmap.width + "|" + param1.clipboard.bitmap.height, true);
                this.clipboard = new Checkbox("rm_new_from_clipboard");
                this.clipboard.addEventListener(ChangeEvent.CHANGE, this.ClipboardChange);
                this.clipboard.x = 20;
                this.clipboard.y = 170;
                addChild(this.clipboard);
                this.transparent.y = 190;
            }
            var _loc_4:* = new Literal("rm_width");
            _loc_4.x = 18;
            _loc_4.y = 120;
            addChild(_loc_4);
            this.tbw = new Dropslider(65, 0, 4000, 640);
            this.tbw.x = 20;
            this.tbw.y = 137;
            addChild(this.tbw);
            var _loc_5:* = new Literal("rm_height");
            _loc_5.x = 98;
            _loc_5.y = 120;
            addChild(_loc_5);
            this.tbh = new Dropslider(65, 0, 4000, 480);
            this.tbh.x = 100;
            this.tbh.y = 137;
            addChild(this.tbh);
            this.preset.Bind();
            addChild(this.preset);
            this.PresetChange();
            return;
        }// end function

        private function PresetChange(... args) : void
        {
            args = this.preset.value.split("|");
            if (args.length > 1)
            {
                this.tbw.Set(int(args[0]));
                this.tbh.Set(int(args[1]));
            }
            return;
        }// end function

        private function ClipboardChange(event:ChangeEvent) : void
        {
            if (this.clipboard.checked)
            {
                this.tbw.Set(ed.clipboard.bitmap.width);
                this.tbh.Set(ed.clipboard.bitmap.height);
            }
            else
            {
                this.PresetChange();
            }
            return;
        }// end function

        override public function OKClick(event:MouseEvent) : void
        {
            var _loc_2:Document = null;
            var _loc_3:BitmapData = null;
            if (!AppState.TextInteraction)
            {
                _loc_2 = new Document(this.tbn.text, Math.max(this.tbw.value, 1), Math.max(this.tbh.value, 1));
                if (!this.transparent.checked || this.clipboard != null && this.clipboard.checked)
                {
                    _loc_3 = new BitmapData(_loc_2.dw, _loc_2.dh, true, this.transparent.checked ? (16777215) : (4294967295));
                    if (this.clipboard != null && this.clipboard.checked)
                    {
                        _loc_3.draw(ed.clipboard.bitmap);
                    }
                }
                _loc_2.AddBitmapLayer(_loc_3);
                ed.AddCanvas(new Canvas(ed, _loc_2));
                ed.canvas.history.Did(new HistoryEntry(Action.New));
                Close();
            }
            return;
        }// end function

        override public function CloseClick(event:MouseEvent) : void
        {
            if (!AppState.TextInteraction)
            {
                Close();
                ed.CheckToShowSplash();
            }
            return;
        }// end function

    }
}
